﻿@inject IHttpService _httpService

@inject ILocalStorageService _localStorage
@inject IPopupService PopupService


@inject NavigationManager NavigationManager


    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="创建标签"/>

<AutoSaveTip TItem="CreateTagViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />

<div>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.EditSummaryTip Type="ExaminedNormalListModelType.Tag" Class="mb-3" Creating/>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="创建标签" Icon="@IconType.Tag.ToIconString()" Outline>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Tags.MainCard Model="Model.Main" Class="mb-4" ShowTagSearch="false" TagItems="TagItems" />
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Tags.ChildEntriesCard Model="Model.Entries" Class="mb-4" EntryItems="EntryItems" />

    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Tags.ChildTagsCard Model="Model.Tags" Class="mb-4" TagItems="TagItems" />

    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
</CnGalWebSite.Components.Containers.MasaTitleContainer>
    }
</div>

@code {
    [Parameter]
    public string Id { get; set; }

    private CreateTagViewModel Model { get; set; } = new CreateTagViewModel();

    private bool isReady = false;
    private List<string> TagItems { get; set; } = new List<string>();
    private List<string> EntryItems { get; set; } = new List<string>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    AutoSaveTip<CreateTagViewModel> autoSave { get; set; }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取所有标签
                TagItems = await _httpService.GetAsync<List<string>>("api/tags/GetAllTags");
                EntryItems = await _httpService.GetAsync<List<string>>("api/home/GetSearchTipList");
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有标签词条名称列表失败");
            }
        }
    }

    private async Task OnSubmit()
    {
        try
        {
            var obj = await _httpService.PostAsync<CreateTagViewModel, Result>("api/tags/CreateTag", Model);
            //判断结果
            if (obj.Successful == false)
            {

                await PopupService.ToastErrorAsync("创建标签失败", obj.Error);
            }
            else
            {
                autoSave.Clean();

                await PopupService.ToastSuccessAsync("创建标签成功", "创建标签成功，等待审核通过");
                NavigationManager.NavigateTo( "/tags/index/" + obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建标签失败");
        }
    }


    private void DataLoaded(CreateTagViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();
        
    }

}
